package com.myh.springcloud.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.myh.springcloud.pojo.User;
import com.myh.springcloud.service.UserService;

/*
**2020年7月4日--下午12:17:13
**@version:
**莫耀华:
**@Description:
*/
@RestController
public class UserController {

	@Autowired
	private UserService userService;
	
	//获取一些配置信息  得到具体的微服务！
	@Autowired
	private DiscoveryClient client;
	
	@RequestMapping("/user/add")
	public boolean addUser(@RequestBody(required = false) User user) {
		System.out.println(user);
		return userService.addUser(user);
	}
	@GetMapping("/user/get/{id}")
	public User get(@PathVariable("id") Long id) {
		return userService.getUserById(id);
	}
	@GetMapping("/user/getAll")
	public List<User> getList() {
		return userService.getlistUser();
	}
	
	//注册进来的微服务  获取一些信息 开启注解  @EnableDiscoveryClient 
	@GetMapping("/user/dscovery")
	public Object dscovery() {
		//获取微服务列表清单
		List<String> services = client.getServices();
		System.out.println("services+="+services);
		
		//得到一个具体的微服务 通过具体的微服务id  spring.application.name
		List<ServiceInstance> list = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
		for (ServiceInstance serviceInstance : list) {
			System.out.println(
					serviceInstance.getHost()+" "+
					serviceInstance.getPort()+" "+	
					serviceInstance.getUri()+" "+	
					serviceInstance.getServiceId()
					);
		}
		
		return this.client;
	}
	
}
